package com.example.coffeeseller_2.model.helper.view_helper;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.widget.CoffeeRoundImage;

/**
 * Created by WH on 2018/8/7.
 * 右侧扫码界面
 */

public class SaomaViewHelper {

    private static String TAG = "SaomaViewHelper";
    private View view;
    private TextView mPayWayTipLayout;
    private ImageView mQrCode;
    private LinearLayout mSaomaSuccessTipLayout;
    private LinearLayout mSaomaTimeOutLayout;
    private LinearLayout mRequestQrLayout;
    private LinearLayout mQrLayout;
    private CoffeeRoundImage mQrCodeBottom;
    private LinearLayout mBgLayout;
    private Button mRequestQrButton;
    private Context context;
    private Handler handler;


    public SaomaViewHelper(Context context) {
        this.context = context;
        handler = new Handler();
        view = LayoutInflater.from(context).inflate(R.layout.include_pay, null, false);
        bindViews(view);
    }

    public View getView(Bitmap bitmap) {
        setQRBitmap(bitmap);
        return view;
    }

    private void bindViews(View view) {

        mPayWayTipLayout = (TextView) view.findViewById(R.id.payWayTipLayout);
        mQrCode = (CoffeeRoundImage) view.findViewById(R.id.qrCode);
        mSaomaSuccessTipLayout = (LinearLayout) view.findViewById(R.id.saomaSuccessTipLayout);
        mSaomaTimeOutLayout = (LinearLayout) view.findViewById(R.id.saomaTimeOutLayout);
        mRequestQrLayout = (LinearLayout) view.findViewById(R.id.requestQrLayout);
        mRequestQrButton = (Button) view.findViewById(R.id.requestQrButton);
        mQrCodeBottom = (CoffeeRoundImage) view.findViewById(R.id.qrCodeBottom);
        mBgLayout = (LinearLayout) view.findViewById(R.id.bgLayout);
        mQrLayout = (LinearLayout) view.findViewById(R.id.qrLayout);
        mRequestQrButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isShowRequestQRcode(false);
            }
        });
    }


    public void setQRBitmap(final Bitmap bitmap) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "setQRBitmap");

                mQrCode.setImageBitmap(bitmap);
                mQrCodeBottom.setImageBitmap(bitmap);
                mQrLayout.setVisibility(View.VISIBLE);
                mRequestQrLayout.setVisibility(View.GONE);
                mSaomaSuccessTipLayout.setVisibility(View.INVISIBLE);

            }
        });


    }

    public void setSaomaTimeOut() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                mSaomaTimeOutLayout.setVisibility(View.VISIBLE);
                mQrCodeBottom.setVisibility(View.VISIBLE);
                mBgLayout.setAlpha(0.9f);
                mQrLayout.setVisibility(View.INVISIBLE);
                mRequestQrLayout.setVisibility(View.GONE);
                mPayWayTipLayout.setVisibility(View.GONE);
            }
        });

    }

    public void clear() {
        view = LayoutInflater.from(context).inflate(R.layout.include_pay, null, false);
        bindViews(view);
        handler.removeCallbacksAndMessages(null);
    }

    /*
    *
    * 设置扫码结果
    * */
    public void setSaomaResult(final boolean isSuccess) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (isSuccess) {
                    mQrCodeBottom.setVisibility(View.VISIBLE);
                    mBgLayout.setAlpha(0.9f);
                    mQrLayout.setVisibility(View.INVISIBLE);
                    mSaomaSuccessTipLayout.setVisibility(View.VISIBLE);
                    mRequestQrLayout.setVisibility(View.GONE);
                    mPayWayTipLayout.setVisibility(View.GONE);
                }
            }
        });

    }

    /*
    * 是否重新扫码
    * */
    public void isShowRequestQRcode(final boolean isRequest) {

        handler.post(new Runnable() {
            @Override
            public void run() {
                if (isRequest) {
                    mQrCodeBottom.setVisibility(View.VISIBLE);
                    mBgLayout.setAlpha(0.9f);
                    mQrLayout.setVisibility(View.INVISIBLE);
                    mRequestQrLayout.setVisibility(View.VISIBLE);
                    mSaomaSuccessTipLayout.setVisibility(View.GONE);
                    mPayWayTipLayout.setVisibility(View.GONE);
                } else {
                    mQrCodeBottom.setVisibility(View.GONE);
                    mQrLayout.setVisibility(View.VISIBLE);
                    mBgLayout.setAlpha(1f);
                    mRequestQrLayout.setVisibility(View.GONE);
                    mSaomaSuccessTipLayout.setVisibility(View.INVISIBLE);
                    mPayWayTipLayout.setVisibility(View.GONE);
                }
            }
        });

    }

}
